package com.hxy.controller;

import com.github.pagehelper.PageInfo;
import com.hxy.common.JsonResult;
import com.hxy.pojo.Branch;
import com.hxy.pojo.Business;
import com.hxy.pojo.vo.BusinessVo;
import com.hxy.service.BusinessService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * business(岗位表)控制
 */
@Controller
@RequestMapping("/business")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    /**
     * 查寻BusinessVo信息(模糊查询及全部分页)
     *
     * @param page
     * @param limit
     * @param businessname
     * @param branchid
     * @param bdetail
     * @return
     */
    @RequestMapping("/showBusinessVo")
    @ResponseBody
    public JsonResult showBusinessVo(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                     @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                                     @RequestParam(value = "businessname", required = false, defaultValue = "") String businessname,
                                     @RequestParam(value = "branchid", required = false, defaultValue = "") Integer branchid,
                                     @RequestParam(value = "bdetail", required = false, defaultValue = "") String bdetail
    ) {
        page = page == null ? 1 : page;
        BusinessVo businessVo = new BusinessVo();
        businessVo.setBusinessname(businessname);
        businessVo.setBranchId(branchid);
        businessVo.setBdetail(bdetail);
        PageInfo<BusinessVo> BusinessVoPageInfo = businessService.selectAllPagerBusinessVo(page, limit, businessVo);

        return new JsonResult(0, "success", BusinessVoPageInfo);

    }

    /**
     * 根据部门id查询包含岗位
     *
     * @param id
     * @return
     */
    @RequestMapping("/selectBusinessNamesbyBeanchId")
    @ResponseBody
    public JsonResult findBusinessNamesByid(Integer id) {
        System.out.println("brandchID:" + id);
        List<Business> businessnames = businessService.findBusinessNamesByid(id);
        return new JsonResult(0, "success", businessnames);
    }

    /**
     * 岗位单条信息回显
     *
     * @param id
     * @return
     */
    @RequestMapping("/selectInfoByBusinesId")
    @ResponseBody
    public JsonResult selectInfoByBusinesId(Integer id) {
        BusinessVo businessVo = businessService.selectInfoByBusines(id);
        if (businessVo != null) {
            return new JsonResult(0, "success", businessVo);
        } else {

        }
        return new JsonResult(0, "success", businessVo);
    }

    /**
     * 更改部门信息,同步更改stuffinfo的部门和岗位信息
     *
     * @param business
     * @return
     */
    @RequestMapping("/updateOne")
    @ResponseBody
    public JsonResult updateOne(BusinessVo business) {
        JsonResult jsonResult = businessService.updateOne(business);
        return jsonResult;
    }

    /**
     * 添加岗位
     * @param business
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public JsonResult add(BusinessVo business) {
        JsonResult jsonResult = businessService.add(business);
        return jsonResult;
    }
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Integer id){
        JsonResult delete = businessService.delete(id);
        return delete;
    }
}
